package handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;


//TODO：复制时更改授权码状态
@WebServlet(name = "makeAu", value = "/makeAu")
public class makeAu extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        PrintWriter out =resp.getWriter();
        String check=req.getParameter("check");
        String au = req.getParameter("au");
            Cookie[] cookies= req.getCookies();
            String username = null;
            if(cookies!=null){
                for (Cookie cookie : cookies) {
                    if(cookie.getName().equals("username")){
                        username= cookie.getValue();
                    }
                }
                try {
                    Refresh.refresh_state1(au);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Refresh.refresh_u_a(au,username);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
    }
}